package com.pet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pet.mapper.UserMapper;
import com.pet.pojo.Comment;
import com.pet.pojo.Topic;
import com.pet.pojo.User;
import com.pet.service.ForumService;
import com.pet.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/*  刘星宇  */
@RestController
@CrossOrigin
@RequestMapping("/forum")
public class ForumController {

    @Autowired
    private ForumService forumService;

    @GetMapping("/topic/findAllTopicList")
    public SysResult findAllTopicList() throws ParseException {
        List<Topic> topicList = forumService.findAllTopicList();
        return SysResult.success(topicList);
    }
    // http://localhost:8081/topic/findTopicById?id=1
    @GetMapping("/topic/findTopicById")
    public SysResult findTopicById(Integer id){
        System.out.println(id);
        Topic topic = forumService.findATopicById(id);
        return SysResult.success(topic);
    }

    @GetMapping("/comment/findAllCommentByTopicId")
    public SysResult findAllCommentByTopicId(Integer id){
        System.out.println(id);
        List<Comment> commentList = forumService.findAllCommentByTopicId(id);
        return SysResult.success(commentList);
    }

    // http://localhost:8081/comment/saveComment
    @PostMapping("/comment/saveComment")
    public SysResult saveComment(@RequestBody Comment comment){
        System.out.println(comment);
        forumService.saveComment(comment);
        return SysResult.success();
    }

    @Autowired
    private UserMapper userMapper;
    @GetMapping("/findUserByUsername")
    public SysResult findUserByUsername(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        return SysResult.success(user);
    }

}
